<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onBeforeMount, nextTick  } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}
const forInit = proxy.$Init || {}
const forFees = proxy.$Fees || []

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = reactive({
  isSelf: Number(logUser.id) === Number(proxy.$route.query.dealer_id),
  // ----
  title: '',
  // ----
  storePayId: 0,
  storeFeeId: 'marketing',
  storeFeeVal: 0,
  storeFeeUnit: '',
  storeFeeUse: true,
  isStore: Number(logUser.is_store) === 1,
  // ----
  forAdm: String(proxy.$Phone) === String(logUser.telphone)
})
// ----
const items = ref([])

/* ---- */
const layerBox = ref()

/* ---- */
const setAppTitle = (title) => {
  document.title = title
  // --利用iframe的onload事件刷新页面--
  const iframe = document.createElement('iframe')
  iframe.style.visibility = 'hidden'
  iframe.style.width = 0
  iframe.style.height = 0
  document.body.appendChild(iframe)
  nextTick(() => { document.body.removeChild(iframe) })
}
// ----
const getCurrencyInfo = async () => {
  if (user.isSelf) {
    let workLine = []
    await proxy.$Myfun.getJsonData(logUser.topupze, (resJson) => { workLine = resJson })
    // --获取购买移动营销充值信息--
    forFees.some((item) => {
      if (String(item.id) === String(user.storeFeeId)) {
        user.storeFeeVal = Number(item.amount)
        user.storeFeeUnit = item.typeText
        user.storeFeeUse = item.isOpenUse
      }
    })
    // ----
    if (user.isStore || !user.storeFeeUse || user.forAdm) {
      proxy.$Myfun.openPage(proxy, 'replace', 'ForStore')
    } else {
      getAuditFun(2)
    }
    // ----
    user.title = '管理移动门店'
  } else {
    items.value = [{
      show: true,
      child: [{
        show: true,
        isArrow: true,
        name: '我的订单',
        callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'MyOrder', { dealer_id: proxy.$route.query.dealer_id }) }
      }]
    }, {
      show: true,
      child: [{
        show: true,
        isArrow: true,
        name: '收货地址',
        callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'UserAddress', { dealer_id: proxy.$route.query.dealer_id }) }
      }]
    }]
    // ----
    user.title = '我的订单管理中心'
  }
  // --设置网页title--
  const forTitle = user.title
  setAppTitle(forTitle)
}
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
    case 2: {
      const options = {
        type: 6,
        shadeClose: false,
        maxCount: 500,
        title: '感谢支持【万域商友会】',
        content: '请交会费：' + user.storeFeeVal.toFixed(2) + '元/' + user.storeFeeUnit,
        button: [{
          text: proxy.$Global.layer.btnCancel,
          callback: () => {
            layerBox.value.close()
            // ----
            proxy.$router.back()
          }
        }, {
          text: '我来交费',
          class: 'xw-color-24',
          callback: (count) => {
            layerBox.value.close()
            // ----
            const lookGiveNum = forInit.look_num || 0
            const releaseGiveNum = forInit.release_num || 0
            const forToPay = { payType: user.storeFeeId, regType: 'jobs', payId: user.storePayId, payIdKey: 'jobs_id', payCount: count }
            const forExtend = { name: 'TransitPage', opts: { forAttr: 'is_store,dealer_views,opport_views', forVal: '1,' + Number(count * lookGiveNum) + ',' + Number(count * releaseGiveNum), forType: 'shopFee' } }
            proxy.$Myfun.openPage(proxy, 'push', 'PaySys', { toPay: JSON.stringify(forToPay), exTend: JSON.stringify(forExtend) })
          }
        }]
      }
      proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
      break
    }
  }
}

/* ---- */
onBeforeMount(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <span v-if="son.msg" v-html="son.msg"></span>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
    <layer-view ref="layerBox"/>
  </div>
</template>

<style scoped>
/* ---- */
.userSys { padding: 0 0 7.4rem; }
</style>